# myapp/views.py
from django.db.backends.ddl_references import Columns
from rest_framework import viewsets, status
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

from appeal.models import Appeals
from column.models import Column
from complaint.models import Complaints
from follow.models import Follow
from user.models import UserProfile
from user.serializer import UserSerializer
from video.models import Videos
from .models import Notification
from .serializer import NotificationSerializer
from notifications.signals import notify


class NotificationViewSet(viewsets.ModelViewSet):
    serializer_class = NotificationSerializer

    def get_queryset(self):
        queryset = Notification.objects.all()
        params = self.request.query_params
        if params:
            filters = {}
            for key, value in params.items():
                filters[key] = value
            queryset = queryset.filter(**filters)
        return queryset

    def get_checked_message_num(self, request, *args, **kwargs):
        user = kwargs.get('user')
        messages = Notification.objects.filter(customer_id=user).filter(checked=False)
        mention_num = messages.filter(category='MENTION').count()
        reply_num = messages.filter(category='REPLY').count()
        like_num = messages.filter(category='LIKE').count()
        system_num = messages.filter(category='SYSTEM').count()
        all_num = mention_num + reply_num + like_num + system_num

        return Response(
            {'all_num': all_num, 'mention_num': mention_num, 'reply_num': reply_num, 'like_num': like_num,
             'system_num': system_num},
            status=status.HTTP_200_OK)

    def put_checked_message_num(self, request, *args, **kwargs):
        user = kwargs.get('user')
        type = kwargs.get('type')
        notifications = Notification.objects.filter(customer_id=user).filter(category=type)
        notifications.update(checked=True)

        return Response(status=status.HTTP_200_OK)

    def send_comment(self, request, *args, **kwargs):
        actor = request.data.get('actor')
        recipient = request.data.get('recipient')
        description = request.data.get('description')
        notify.send(sender=UserProfile.objects.get(id=actor), recipient=UserProfile.objects.get(id=recipient),
                    verb='verb', customer_id=recipient, sender_id=actor, description=description, category='REPLY')
        return Response(status=status.HTTP_200_OK)

    def send_comment_comment(self, request, *args, **kwargs):
        actor = request.data.get('actor')
        recipient = request.data.get('recipient')
        description = request.data.get('description')
        root_parent_content = request.data.get('root_parent_content')
        direct_parent_content = request.data.get('direct_parent_content')
        notify.send(sender=UserProfile.objects.get(id=actor), recipient=UserProfile.objects.get(id=recipient),
                    verb='verb', customer_id=recipient, sender_id=actor, description=description, category='REPLY',
                    root_parent_content=root_parent_content, direct_parent_content=direct_parent_content)
        return Response(status=status.HTTP_200_OK)

    def send_like(self, request, *args, **kwargs):
        actor = request.data.get('actor')
        recipient = request.data.get('recipient')
        root_parent_content = request.data.get('root_parent_content')
        notify.send(sender=UserProfile.objects.get(id=actor), recipient=UserProfile.objects.get(id=recipient),
                    verb='verb', customer_id=recipient, sender_id=actor, category='LIKE',
                    root_parent_content=root_parent_content)
        return Response(status=status.HTTP_200_OK)

    def send_at(self, request, *args, **kwargs):
        actor = request.data.get('actor')
        recipient = request.data.get('recipient')
        description = request.data.get('description')
        root_parent_content = request.data.get('root_parent_content')
        direct_parent_content = request.data.get('direct_parent_content')
        notify.send(sender=UserProfile.objects.get(id=actor), recipient=UserProfile.objects.get(id=recipient),
                    verb='verb', customer_id=recipient, sender_id=actor, description=description, category='MENTION',
                    root_parent_content=root_parent_content, direct_parent_content=direct_parent_content)
        return Response(status=status.HTTP_200_OK)

    def send_video_reviewing_passed(self, request, *args, **kwargs):
        video_id = request.data.get('video')
        video = Videos.objects.filter(vid=video_id)[0]
        mup = UserProfile.objects.get(id=video.mup.id)
        description_up = "您上传的视频审核已通过,快去看看吧！  ===><a aria-current='page' href='/video/" + video.vid + "' target='_blank'>" + video.title + "</a>"
        description_fan = "您关注的up主：" + mup.username + "发布了新视频，快去看看吧！ ===> <a aria-current='page' href='/video/" + video.vid + "' target='_blank'>" + video.title + "</a>"
        sender_id = 1
        fans = Follow.objects.filter(mup_id=mup.id).filter(is_active=True)
        notify.send(sender=UserProfile.objects.get(id=1), recipient=UserProfile.objects.get(id=mup.id),
                    verb='verb', customer_id=mup.id, sender_id=sender_id, description=description_up, category='SYSTEM')

        for fan in fans:
            notify.send(sender=UserProfile.objects.get(id=1), recipient=UserProfile.objects.get(id=fan.fan.id),
                        verb='verb', customer_id=fan.fan.id, sender_id=sender_id, description=description_fan,
                        category='SYSTEM')

        return Response(status=status.HTTP_200_OK)

    def send_video_reviewing_not_passed(self, request, *args, **kwargs):
        video_id = request.data.get('video')
        video = Videos.objects.filter(vid=video_id)[0]
        mup = UserProfile.objects.get(id=video.mup.id)
        description_up = "您上传的视频违反了相关法律规定,审核未通过!!  ===> <a aria-current='page' href='/create/appeal' target='_blank'>" + video.title + "</a>"
        sender_id = 1
        notify.send(sender=UserProfile.objects.get(id=1), recipient=UserProfile.objects.get(id=mup.id),
                    verb='verb', customer_id=mup.id, sender_id=sender_id, description=description_up, category='SYSTEM')

        return Response(status=status.HTTP_200_OK)

    def send_column_created(self, request, *args, **kwargs):
        coid = request.data.get('coid')
        column = Column.objects.filter(coid=coid)[0]
        mup = UserProfile.objects.get(id=column.mup.id)
        description_fan = "你关注的up主" + mup.username + "创建了新的专栏: " + column.name + "，快去围观吧"
        sender_id = 1
        fans = Follow.objects.filter(mup_id=mup.id).filter(is_active=True)
        for fan in fans:
            notify.send(sender=UserProfile.objects.get(id=1), recipient=UserProfile.objects.get(id=fan.fan.id),
                        verb='verb', customer_id=fan.fan.id, sender_id=sender_id, description=description_fan,
                        category='SYSTEM')
        return Response(status=status.HTTP_200_OK)

    def send_video_complaint_pass(self, request, *args, **kwargs):
        video_id = request.data.get('video')
        video = Videos.objects.filter(vid=video_id)[0]
        sender_id = 1
        mup = UserProfile.objects.get(id=video.mup.id)
        complainters = Complaints.objects.filter(video_id=video.vid).filter(type=1)
        description_up = "您的视频 " + "<a aria-current='page' href='/create/appeal' target='_blank'>" + video.title + "</a>" + " 收到举报并且举报成功，现已下架"
        description_fan = "您对视频 <strong>" + video.title + " </strong>的举报通过，感谢您对维护社区环境做出的贡献"

        notify.send(sender=UserProfile.objects.get(id=1), recipient=UserProfile.objects.get(id=mup.id),
                    verb='verb', customer_id=mup.id, sender_id=sender_id, description=description_up, category='SYSTEM')

        for fan in complainters:
            notify.send(sender=UserProfile.objects.get(id=1), recipient=UserProfile.objects.get(id=fan.user.id),
                        verb='verb', customer_id=fan.user.id, sender_id=sender_id, description=description_fan,
                        category='SYSTEM')

        return Response(status=status.HTTP_200_OK)

    def send_video_complaint_not_pass(self, request, *args, **kwargs):
        video_id = request.data.get('video')
        video = Videos.objects.filter(vid=video_id)[0]
        sender_id = 1
        complainters = Complaints.objects.filter(video_id=video.vid).filter(type=1)
        description_fan = "很抱歉，您对视频 <strong>" + video.title + " </strong>的举报未通过，感谢您对维护社区环境做出的贡献"

        for fan in complainters:
            notify.send(sender=UserProfile.objects.get(id=1), recipient=UserProfile.objects.get(id=fan.user.id),
                        verb='verb', customer_id=fan.user.id, sender_id=sender_id, description=description_fan,
                        category='SYSTEM')

        return Response(status=status.HTTP_200_OK)

    def send_column_complaint_pass(self, request, *args, **kwargs):
        column_id = request.data.get('column')
        column = Column.objects.filter(coid=column_id)[0]
        sender_id = 1
        mup = UserProfile.objects.get(id=column.mup.id)
        complainters = Complaints.objects.filter(column__coid=column.coid).filter(type=2)
        description_up = "您的专栏 " + "<a aria-current='page' href='/create/appeal' target='_blank'>" + column.name + "</a>" + " 收到举报并且举报成功，现已下架"
        description_fan = "您对专栏 <strong>" + column.name + " </strong>的举报通过，感谢您对维护社区环境做出的贡献"

        notify.send(sender=UserProfile.objects.get(id=1), recipient=UserProfile.objects.get(id=mup.id),
                    verb='verb', customer_id=mup.id, sender_id=sender_id, description=description_up, category='SYSTEM')

        for fan in complainters:
            notify.send(sender=UserProfile.objects.get(id=1), recipient=UserProfile.objects.get(id=fan.user.id),
                        verb='verb', customer_id=fan.user.id, sender_id=sender_id, description=description_fan,
                        category='SYSTEM')

        return Response(status=status.HTTP_200_OK)

    def send_column_complaint_not_pass(self, request, *args, **kwargs):
        column_id = request.data.get('column')
        column = Column.objects.filter(coid=column_id)[0]
        sender_id = 1
        complainters = Complaints.objects.filter(column__coid=column.coid).filter(type=2)
        description_fan = "很抱歉，您对专栏 <strong>" + column.name + " </strong>的举报未通过，感谢您对维护社区环境做出的贡献"

        for fan in complainters:
            notify.send(sender=UserProfile.objects.get(id=1), recipient=UserProfile.objects.get(id=fan.user.id),
                        verb='verb', customer_id=fan.user.id, sender_id=sender_id, description=description_fan,
                        category='SYSTEM')

        return Response(status=status.HTTP_200_OK)

    def send_video_appeal(self, request, *args, **kwargs):
        video_id = request.data.get('video')
        type = request.data.get('type')
        video = Videos.objects.filter(vid=video_id)[0]
        mup = UserProfile.objects.get(id=video.mup.id)
        sender_id = 1
        if type == 0:
            description_up = "你的视频申诉未通过，点击查看" + "<a aria-current='page' href='/create/appeal' target='_blank'>" + video.title + "</a>"
            notify.send(sender=UserProfile.objects.get(id=1), recipient=UserProfile.objects.get(id=mup.id),
                        verb='verb', customer_id=mup.id, sender_id=sender_id, description=description_up,
                        category='SYSTEM')
        elif type == 1:
            description_up = "你的视频申诉通过，点击查看" + "<a aria-current='page' href='/video/" + video.vid + "' target='_blank'>" + video.title + "</a>"
            notify.send(sender=UserProfile.objects.get(id=1), recipient=UserProfile.objects.get(id=mup.id),
                        verb='verb', customer_id=mup.id, sender_id=sender_id, description=description_up,
                        category='SYSTEM')

        return Response(status=status.HTTP_200_OK)

    def send_column_appeal(self, request, *args, **kwargs):
        column_id = request.data.get('column')
        type = request.data.get('type')
        column = Column.objects.filter(coid=column_id)[0]
        mup = UserProfile.objects.get(id=column.mup.id)
        sender_id = 1
        if type == 0:
            description_up = "你的专栏申诉未通过，点击查看" + "<a aria-current='page' href='/create/appeal' target='_blank'>" + column.name + "</a>"
            notify.send(sender=UserProfile.objects.get(id=1), recipient=UserProfile.objects.get(id=mup.id),
                        verb='verb', customer_id=mup.id, sender_id=sender_id, description=description_up,
                        category='SYSTEM')
        elif type == 1:
            description_up = "恭喜，你的专栏申诉通过"
            notify.send(sender=UserProfile.objects.get(id=1), recipient=UserProfile.objects.get(id=mup.id),
                        verb='verb', customer_id=mup.id, sender_id=sender_id, description=description_up,
                        category='SYSTEM')

        return Response(status=status.HTTP_200_OK)

    def send_video_change(self, request, *args, **kwargs):
        video_id = request.data.get('video')
        type = request.data.get('type')
        video = Videos.objects.filter(vid=video_id)[0]
        mup = UserProfile.objects.get(id=video.mup.id)
        sender_id = 1
        if type == 0:
            description_up = "你的视频被下架，点击查看" + "<a aria-current='page' href='/create/appeal' target='_blank'>" + video.title + "</a>"
            notify.send(sender=UserProfile.objects.get(id=1), recipient=UserProfile.objects.get(id=mup.id),
                        verb='verb', customer_id=mup.id, sender_id=sender_id, description=description_up,
                        category='SYSTEM')
        elif type == 1:
            description_up = "你的视频已恢复，点击查看" + "<a aria-current='page' href='/video/" + video.vid + "' target='_blank'>" + video.title + "</a>"
            notify.send(sender=UserProfile.objects.get(id=1), recipient=UserProfile.objects.get(id=mup.id),
                        verb='verb', customer_id=mup.id, sender_id=sender_id, description=description_up,
                        category='SYSTEM')

        return Response(status=status.HTTP_200_OK)

    def send_column_change(self, request, *args, **kwargs):
        column_id = request.data.get('column')
        type = request.data.get('type')
        column = Column.objects.filter(coid=column_id)[0]
        mup = UserProfile.objects.get(id=column.mup.id)
        sender_id = 1
        if type == 0:
            description_up = "你的专栏被下架，点击查看" + "<a aria-current='page' href='/create/appeal' target='_blank'>" + column.name + "</a>"
            notify.send(sender=UserProfile.objects.get(id=1), recipient=UserProfile.objects.get(id=mup.id),
                        verb='verb', customer_id=mup.id, sender_id=sender_id, description=description_up,
                        category='SYSTEM')
        elif type == 1:
            description_up = "你的专栏<strong> " + column.name + "</strong>已恢复"
            notify.send(sender=UserProfile.objects.get(id=1), recipient=UserProfile.objects.get(id=mup.id),
                        verb='verb', customer_id=mup.id, sender_id=sender_id, description=description_up,
                        category='SYSTEM')

        return Response(status=status.HTTP_200_OK)

    def sentFan(self, request, *args, **kwargs):
        mup_id = request.data.get('mup')
        fan_id = request.data.get('fan')
        mup = UserProfile.objects.get(id=mup_id)
        fan = UserProfile.objects.get(id=fan_id)
        sender_id = 1
        description_up = "<a aria-current='page' href='/profile/" + str(fan_id) + "'" + " target='_blank'>" + fan.username + "</a>" + "关注了你，快去看看吧~"
        notify.send(sender=UserProfile.objects.get(id=1), recipient=UserProfile.objects.get(id=mup.id),
                    verb='verb', customer_id=mup.id, sender_id=sender_id, description=description_up,
                    category='SYSTEM')
        return Response(status=status.HTTP_200_OK)
